﻿using System;
namespace Athena.Message
{
    public class RpcTransportMessage
    {
        public RpcTransportMessage()
        {

        }

        public RpcTransportMessage(object content)
        {
            Content = content ?? throw new ArgumentNullException(nameof(content));
            ContentType = content.GetType().FullName;
        }

        public RpcTransportMessage(object content, string fullName)
        {
            Content = content ?? throw new ArgumentException(nameof(content));
            ContentType = fullName;
        }

        public string Id { get; set; }

        public object Content { get; set; }

        public string ContentType { get; set; }

        public bool IsInvokeMessage {
            get {
                return ContentType == StaticRpcInvokeMessageTypeName.RpcInvokeMessageTypeName;
            }
        }

        public bool IsInvokeResultMessage {
            get {
                return ContentType == StaticRpcInvokeMessageTypeName.RpcInvokeResultMessageTypeName;
            }
        }

        public T GetContent<T>()
        {
            return (T)Content;
        }



        public static RpcTransportMessage CreateRpcInvokeMessage(RpcInvokeMessage rpcInvokeMessage)
        {
            return new RpcTransportMessage(rpcInvokeMessage, StaticRpcInvokeMessageTypeName.RpcInvokeMessageTypeName)
            {
                Id = Guid.NewGuid().ToString("N"),
            };
        }

        public static RpcTransportMessage CreateRpcInvokeResultMessage(string transportMessageId, RpcInvokeResultMessage rpcInvokeResultMessage)
        {
            return new RpcTransportMessage(rpcInvokeResultMessage, StaticRpcInvokeMessageTypeName.RpcInvokeResultMessageTypeName)
            {
                Id = transportMessageId
            };
        }
    }
}
